class DeclarationPayer < ActiveRecord::Base
  belongs_to :user
  belongs_to :declaration
  
  attr_reader :additional_persons 
  attr_accessor :do_not_remove_before_save
  
  defaults :additional_persons => 0
  
  def share
    self.declaration.share(self.user.id)
  end

  def share_as_money()
    share = self.share
    Money.new(share)
  end
  
  def additional_persons
    self.attributes['additional_persons'] ||= 0
  end
  
  def self.all_users
    declaration_payers = Array.new
    users = User.get_all_active
    
    users.each { |user|
      declaration_payer = DeclarationPayer.new
      declaration_payer.user = user
      declaration_payers << declaration_payer
      declaration_payer.declaration = Declaration.new
    }
    return declaration_payers
  end
  
  def self.all_users_with_edit
    retval = {}
    declaration_payers = DeclarationPayer.all_users
    
    declaration_payers.each { | declaration_payer |
      retval[declaration_payer.user_id.to_i] =  {'declaration_payer' => declaration_payer, 'edit' => false }
    }
    
    return retval    
  end
  
  def do_not_remove_before_save
    return false if self.new_record? == true && @do_not_remove_before_save.nil?
    true
  end

end
